﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using ValuationInterface;
using ValuationModel;

namespace ValuationDAL
{
    public class ProjectDAL
    {
        #region 立项显示
        /// <summary>
        /// 立项显示
        /// </summary>
        /// <returns></returns>
        public List<EABasic_Info> ProjectApprovalList()
        {
            string sql = "select a.projectNumber,a.the_Department,b.loan_Name,b.loan_TEL,a.basic_Quota,a.RegisterTime,a.basic_State from EABasic_Info a join EALoan_Info b on a.projectNumber=b.projectNumber";
            return DBHelper.ExecuteObjects<EABasic_Info>(sql);
        }
        #endregion

        #region 登记   

        //添加借款人信息
        public int AddLoan(EALoan_Info Loan)
        {
            StringBuilder sbl = new StringBuilder();
            sbl.Append("insert into EALoan_Info(loan_Name,Loan_TEL,loan_IdCard,loan_Contacts,loan_ContactsTEL,projectNumber) values('" + Loan.loan_Name + "','" + Loan.loan_TEL + "','" + Loan.loan_IdCard + "','"+Loan.loan_Contacts+"','"+Loan.loan_ContactsTEL+"','" + Loan.projectNumber + "')");
            return DBHelper.ExecuteNonQuery(sbl.ToString());
        }

        //添加登记信息
        public int AddEntity(EABasic_Info Basic)
        {
            string sql=@"insert into EABasic_Info(ProjectNumber,surplus_Years,basic_Quota,house_Located
                        ,house_Purpose,house_Time,yers_Source,presentation_Type,
                        assessment,the_Department,survey,evaluation,issue_Time) values
                        ('" + Basic.projectNumber + "','" + Basic.surplus_Years + "','" + Basic.basic_Quota + "','" + Basic.house_Located + "'," + Basic.house_Purpose + ",'" + Basic.house_Time + "'," + Basic.yers_Source + ",'" + Basic.presentation_Type + "','" + Basic.assessment + "'," + Basic.the_Department + "," + Basic.survey + "," + Basic.evaluation + ",'" + Basic.issue_Time + "')";

            return DBHelper.ExecuteNonQuery(sql);
        }

        // 获取借款人编号
        public List<EALoan_Info> List()
        {
            string sql = "select max(loan_Id) loan_Id from EALoan_Info ";
            return DBHelper.ExecuteObjects<EALoan_Info>(sql);
        }


        #endregion

        #region 根据项目号查看详情
        /// <summary>
        /// 根据项目号查看详情
        /// </summary>
        /// <returns></returns>
        public DengJi BackDatas(string pnum)
        {
            string sql = @"select *
                            from EABasic_Info a join SysDepartment b
                            on a.The_Department=b.Department_Id join EALoan_Info c
                            on a.ProjectNumber=c.ProjectNumber
                            where a.ProjectNumber='" + pnum + "'";
            return DBHelper.ExecuteObject<DengJi>(sql);
        }
        #endregion

        #region 询价添加，显示
        /// <summary>
        /// 询价添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool InsertEAInquiry<T>(T t)
        {
            return DBHelper.InsertEAInquiry<T>(t);
        }

        //询价显示
        public List<EAInquiry> EAInquiryList()
        {
            string sql = "select * from dbo.EAInquiry";
            return DBHelper.ExecuteObjects<EAInquiry>(sql);
        }
        #endregion

        #region  审核
        public bool AUpdateEntity<T>(T t)
        {
            return DBHelper.AUpdateEnity<T>(t);
        }
        #endregion

        #region 批量添加图片
        /// <summary>
        /// 批量添加图片
        /// </summary>
        /// <param name="photo"></param>
        /// <returns></returns>
        public int Upload(EAPhoto photo)
        {
            string PhotoName = photo.photo_Name;
            string[] Name = PhotoName.Split(',');

            string PhotoUrl = photo.photo_Url;
            string[] url = PhotoUrl.Split('=');

            StringBuilder bui = new StringBuilder();

            if (Name.Length > 0 && url.Length > 0)
            {
                foreach (var item in Name)
                {
                    foreach (var item1 in url)
                    {
                        bui.Append("insert into EAPhoto(ProjectNumber,Photo_Name,Photo_Url) values('" + photo.projectNumber + "','" + item + "','" + item1 + "=" + "')");
                        break;
                    }

                }
            }
            return DBHelper.ExecuteNonQuery(bui.ToString());
        }
        #endregion
       
    }
}
